﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace PendingTransactions
{
    /// <summary>
    /// Interaction logic for dlgContinuity.xaml
    /// </summary>
    public partial class dlgContinuity : Window
    {
        private SalesManagerEntities _context = new SalesManagerEntities();
        private int _ownerID;

        public dlgContinuity(SalesManagerEntities context, int ownerID)
        {
            _context = context;
            _ownerID = ownerID;
            InitializeComponent();
        }

        private void btnProcess_Click(object sender, RoutedEventArgs e)
        {
            Cursor saveCursor = Cursor;
            Cursor = Cursors.Wait;

            _context.spCreateOrdersForSequences(_ownerID, dtAsOf.SelectedDate);

            Cursor = saveCursor;
            Close();
        }

        public static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj == null) return null;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                var child = VisualTreeHelper.GetChild(depObj, i);

                var result = (child as T) ?? GetChildOfType<T>(child);
                if (result != null) return result;
            }
            return null;
        }

        private void dtAsOf_Loaded(object sender, RoutedEventArgs e)
        {
            var dp = sender as DatePicker;
            if (dp == null) return;

            var tb = GetChildOfType<DatePickerTextBox>(dp);
            if (tb == null) return;

            var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
            if (wm == null) return;

            wm.Content = "As Of:";
        }
    }
}
